<?php

declare(strict_types=1);
/*
 * @Description: 个推service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-02-02 12:21:26
 */

namespace App\Service\Push;

use App\Common\RedisCache;
use Yurun\Util\HttpRequest;

class Getui
{
    protected $serverInfo = [];
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
    }

    /**
     * 推送所有人
     */
    public function pushAllTask(?array $data = null): array
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $token = $this->getAuthToken();
            $url = 'https://restapi.getui.com/v2/' . $this->serverInfo['getui_app_id'] . '/push/all';
            $post['audience']['cid'] = (is_array($data['single_cid'])) ? $data['single_cid'] : explode(',', $data['single_cid']);
            $post['request_id'] = createULId();
            $post['settings']['ttl'] = 3600000;
            $post['settings']['strategy']['default'] = 4;
            $notice['body'] = $data['content'];
            $notice['title'] = $data['title'];
            if (isset($data['channel_level'])) {
                $notice['channel_level'] = $data['channel_level'];
            }
            if (isset($this->serverInfo['logo_url']) && !empty($this->serverInfo['logo_url'])) {
                $notice['logo_url'] = $this->serverInfo['logo_url'];
            }
            $notice['click_type'] = (isset($data['click_type']) && !empty($data['click_type'])) ? $data['click_type'] : 'payload';
            switch ($notice['click_type']) {
                case 'intent':
                    $notice['intent'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                case 'url':
                    $notice['url'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                case 'payload':
                case 'payload_custom':
                    $notice['payload'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                default:
                    # code...
                    break;
            }
            $post['push_message']['notification']= $notice;
            $http = new HttpRequest;
            $response = $http->header('token', $token)->post($url, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            $return['send_error'] = '(' . $responseData['code'] . ') ' . $responseData['msg'];
            $return['pushParam'] = $post;
            $return['task_id'] = '';
            if (isset($responseData['code']) && $responseData['code'] == 0) {
                $return['send_result'] = true;
                $return['send_error'] = '';
                $return['task_id'] = $responseData['data']['taskid'];
            }
            unset($post, $url, $response, $pushData, $http, $data);
        }
        return $return;
    }
    /**
     * 群推送 消息任务发送
     */
    public function listPushStartPage(?array $data = null): array
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $token = $this->getAuthToken();
            $url = 'https://restapi.getui.com/v2/' . $this->serverInfo['getui_app_id'] . '/push/list/cid';
            $post['audience']['cid'] = (is_array($data['single_cid'])) ? $data['single_cid'] : explode(',', $data['single_cid']);
            $post['taskid'] = $data['taskid'];
            $http = new HttpRequest;
            $response = $http->header('token', $token)->post($url, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            if (isset($responseData['code']) && $responseData['code'] == 0) {
                $return['send_result'] = true;
            }
        }
        return $return;
    }
    /**
     * 单条推送 打开页面
     * 
     */
    public function singlePushStartPage(?array $data = null)
    {
        $return['send_result'] = false;
        if (!empty($data) && is_array($data)) {
            $token = $this->getAuthToken();
            $url = 'https://restapi.getui.com/v2/' . $this->serverInfo['getui_app_id'] . '/push/single/cid';
            $post['request_id'] = createULId();
            $post['settings']['ttl'] = 3600000;
            $post['settings']['strategy']['default'] = 4;
            $post['audience']['cid'] = (is_array($data['single_cid'])) ? $data['single_cid'] : explode(',',$data['single_cid']);
            $notice['body'] = $data['content'];
            $notice['title'] = $data['title'];
            if (isset($data['channel_level'])) {
                $notice['channel_level'] = $data['channel_level'];
            }
            if (isset($this->serverInfo['logo_url']) && !empty($this->serverInfo['logo_url'])) {
                $notice['logo_url'] = $this->serverInfo['logo_url'];
            }
            $notice['click_type'] = (isset($data['click_type']) && !empty($data['click_type']))?$data['click_type']:'payload';
            switch ($notice['click_type']) {
                case 'intent':
                    $notice['intent'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                case 'url':
                    $notice['url'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                case 'payload':
                case 'payload_custom':
                    $notice['payload'] = (isset($data['click_type_content']) && !empty($data['click_type_content'])) ? $data['click_type_content'] : '';
                    break;
                default:
                    # code...
                    break;
            }
            $post['push_message']['notification']= $notice;
            $http = new HttpRequest;
            $response = $http->header('token', $token)->post($url, $post, 'json');
            $responseData = json_decode($response->body(), true);
            $return['send_result_data'] = $responseData;
            $return['send_error'] = '('.$responseData['code'].') '. $responseData['msg'];
            $return['pushParam'] = $post;
            $return['task_id'] = '';
            if (isset($responseData['code']) && $responseData['code'] == 0) {
                $return['send_result'] = true;
                $return['send_error'] = '';
                foreach ($responseData['data'] as $task => $vo) {
                    $return['task_id'] = $task;
                }
            }
            unset($post,$url,$response,$pushData,$http,$data);
            return $return;
        }
    }
    private function getAuthToken()
    {
        $redisCache = new RedisCache();
        $signStr = $redisCache->getSetAuthToken($this->serverInfo['_id'], $this->serverInfo['getui_app_id']);
        if (empty($signStr)) {
            $url = 'https://restapi.getui.com/v2/' . $this->serverInfo['getui_app_id'] . '/auth';
            $post['timestamp'] = time() * 1000;
            $post['appkey'] = $this->serverInfo['getui_app_key'];
            $post['sign'] = hash("sha256", $this->serverInfo['getui_app_key'] . $post['timestamp'] . $this->serverInfo['getui_master_secret']);
            $http = new HttpRequest;
            $response = $http->post($url, $post, 'json');
            $responseData = json_decode($response->body(), true);
            if (isset($responseData['code']) && $responseData['code'] == 0) {
                $signStr = $responseData['data']['token'];
                $expire = intval($responseData['data']['expire_time']/1000 - time() - 120);
                $redisCache->getSetAuthToken($this->serverInfo['_id'], $this->serverInfo['getui_app_id'],$signStr,$expire);
            }
        }
        return $signStr;
    }
}
